/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * infoLabel.java
 *
 * Created on 29.03.2010, 14:02:05
 */
package GUI.iFrames;

import java.util.Vector;

import DATA.iDataSetParts.IdInfo;
import GUI.iFrames.DataSourceComponents.DataSourcesInternalFrame;

/**
 * 
 * @author Bunkowski
 */
public class infoLabelInternalFrame extends javax.swing.JInternalFrame {

	public static final long serialVersionUID = 1L;

	private DataSourcesInternalFrame dsif;

	/** Creates new form infoLabel */
	public infoLabelInternalFrame(DataSourcesInternalFrame dsif) {
		this.dsif = dsif;
		initComponents();
		setLayer(2);
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
	 * content of this method is always regenerated by the Form Editor.
	 */
	private void initComponents() {

		buttonGroup1 = new javax.swing.ButtonGroup();
		overwriteButton = new javax.swing.JToggleButton();
		newButton = new javax.swing.JToggleButton();
		infoTextfield = new javax.swing.JTextField();
		okButton = new javax.swing.JButton();
		jSeparator1 = new javax.swing.JSeparator();
		jLabel1 = new javax.swing.JLabel();
		nameTextfield = new javax.swing.JTextField();

		setClosable(true);
		setTitle("Add information");

		buttonGroup1.add(overwriteButton);
		overwriteButton.setText("overwrite selection");
		overwriteButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				overwriteButtonActionPerformed(evt);
			}
		});

		buttonGroup1.add(newButton);
		newButton.setSelected(true);
		newButton.setText("create new");
		newButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				newButtonActionPerformed(evt);
			}
		});

		okButton.setText("ok");
		okButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				okButtonActionPerformed(evt);
			}
		});

		jLabel1.setText("Infomation name:");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(newButton)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(overwriteButton))
												.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 218,
														Short.MAX_VALUE)
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addGroup(
																						javax.swing.GroupLayout.Alignment.LEADING,
																						layout.createSequentialGroup()
																								.addComponent(jLabel1)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										nameTextfield,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										80,
																										Short.MAX_VALUE))
																				.addComponent(
																						infoTextfield,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						169, Short.MAX_VALUE))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(okButton))).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(jLabel1)
										.addComponent(nameTextfield, javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
						.addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(infoTextfield, javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(okButton))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(newButton).addComponent(overwriteButton)).addContainerGap()));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_okButtonActionPerformed
		int[] selectionArray = dsif.table.getSelectedRows();
		Vector<Integer> selection = new Vector<Integer>();
		for (int i = 0; i < selectionArray.length; i++) {
			int j = selectionArray[i];
			selection.add(j);
		}
		int column = dsif.table.getSelectedColumn();
		int infoShift = dsif.idataset.infoShift;
		Vector<String> MIds = dsif.getColumnAt(1);
		if (newButton.isSelected()) {
			String name = nameTextfield.getText();

			Vector<IdInfo> IdInfos = new Vector<IdInfo>();
			for (int i = 0; i < dsif.table.getRowCount(); i++) {
				Vector<String> temp = new Vector<String>();
				if (selection.contains(i)) {
					temp.add(infoTextfield.getText());
				} else {
					temp.add("no info");
				}
				String MId = MIds.get(i);
				IdInfos.add(new IdInfo(MId, temp));
			}
			dsif.idataset.addMeasurementIdInfos(IdInfos, new String[] { name });
			dsif.updateTable();
			this.dispose();
		}
		if (overwriteButton.isSelected()) {
			if (dsif.table.getSelectedColumnCount() > 1) {
				System.out.println("Choose only one column");
			} else {
				for (int i = 0; i < dsif.table.getRowCount(); i++) {
					if (selection.contains(i)) {
						String MId = MIds.get(i);
						dsif.idataset.measurementInfos.get(MId).set(column - infoShift, infoTextfield.getText());
					}
				}
				dsif.updateTable();
				this.dispose();
			}
		}
	}// GEN-LAST:event_okButtonActionPerformed

	private void overwriteButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_overwriteButtonActionPerformed
		nameTextfield.setText(dsif.getHeaderNameAt(dsif.table.getSelectedColumn()));
		nameTextfield.setEditable(false);
	}// GEN-LAST:event_overwriteButtonActionPerformed

	private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_newButtonActionPerformed
		nameTextfield.setEditable(true);
	}// GEN-LAST:event_newButtonActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.ButtonGroup buttonGroup1;
	private javax.swing.JTextField infoTextfield;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JTextField nameTextfield;
	private javax.swing.JToggleButton newButton;
	private javax.swing.JButton okButton;
	private javax.swing.JToggleButton overwriteButton;
	// End of variables declaration//GEN-END:variables
}
